package location

import (
	"device-admin/config/mysql"
	"device-admin/internal/models"
	"device-admin/internal/pkg/jieba"

	"192.168.1.75/go-pkg/errorx"
)

type Robot struct{}

// GetRobotFloor 获取设备所在楼层
func (Robot) GetRobotFloor(robotID string) (floor models.Floor, err error) {
	db := mysql.NewDB()
	// 获取设备位置信息
	var floorDevice models.FloorDevice
	{
		rowsAffected := db.Model(&models.FloorDevice{}).
			Where("robot_id = ?", robotID).
			Take(&floorDevice).
			RowsAffected
		if rowsAffected <= 0 {
			return floor, errorx.New("设备未绑定位置点", -1)
		}
	}
	// 获取设备所在楼层信息
	{
		rowsAffected := db.Model(&models.Floor{}).
			Where("id = ?", floorDevice.FloorID).
			Take(&floor).
			RowsAffected
		if rowsAffected <= 0 {
			return floor, errorx.New("设备未绑定位置点", -1)
		}
	}
	return floor, nil
}

// GetNavMap 获取导航地图
func (r Robot) GetNavMap(robotID string) (floor models.Floor, err error) {
	// 获取设备所在楼层
	floor, err = r.GetRobotFloor(robotID)
	if err != nil {
		return floor, err
	}
	return floor, nil
}

// GetLocationFloorList 获取楼层
func (r Robot) GetLocationFloorList(robotID string) (floors []models.Floor, err error) {
	db := mysql.NewDB()
	// 获取设备所在楼层
	floor, err := r.GetRobotFloor(robotID)
	if err != nil {
		return floors, err
	}
	// 获取楼层
	db.Model(&models.Floor{}).
		Where("location_id = ?", floor.LocationID).
		Order("sort").
		Find(&floors)
	return floors, nil
}

// GetNavPositionList 获取位置点
func (Robot) GetNavPositionList(robotID string) ([]Position, error) {
	data, err := FloorPosition{}.List(robotID, "", "", "1", "", "", false)
	return data, err
}

// GetFloorBrandList 获取楼层品牌
func (r Robot) GetFloorBrandList(robotID string, locationID string, floorID string) (brands []BrandInfo, err error) {
	db := mysql.NewDB()

	// 获取设备所在楼层
	var floor models.Floor
	if robotID != "" {
		floor, err = r.GetRobotFloor(robotID)
		if err != nil {
			return brands, err
		}
		locationID = floor.LocationID
	}
	// 所属楼层和商场不能同时为空
	if locationID == "" && floorID == "" {
		return []BrandInfo{}, errorx.New("所属楼层和商场不能同时为空", -1)
	}
	// 获取楼层品牌
	{
		tx := db.Model(&models.FloorBrand{}).
			Joins("JOIN floor ON floor.id = floor_brand.floor_id").
			Joins("JOIN brand ON brand.id = floor_brand.brand_id").
			Joins("LEFT JOIN floor_position ON floor_position.id = floor_brand.position_id").
			Select(
				"floor_brand.id as bind_id",
				"brand.name as `name`",
				"brand.logo as `logo`",
				"brand.desc as `desc`",
				"brand.id as brand_id",
				"floor.name as floor_name",
				"floor_brand.floor_id",
				"floor_brand.contact_phone",
				"floor_brand.shop_no",
				"floor_brand.recommended",
				"floor_brand.search_recommended",
				"floor_brand.recommended_sort",
				"floor_brand.search_recommended_sort",
				"floor_position.name as `position_name`",
				"floor_position.id as `position_id`",
			)
		if floorID != "" {
			tx = tx.Where("floor.id = ?", floorID)
		} else {
			tx = tx.Where("floor.location_id = ?", locationID)
		}
		tx.Find(&brands)
		return brands, nil
	}
}

// GetRecommendedBrandList 获取推荐品牌
func (Robot) GetRecommendedBrandList(robotID string) (brands []BrandInfo, err error) {
	list, err := Floor{}.GetRecommendedBrand(robotID, "", 1)
	return list, err
}

// GetSearchRecommendedBrandList 获取搜索推荐品牌
func (Robot) GetSearchRecommendedBrandList(robotID string) (brands []BrandInfo, err error) {
	list, err := Floor{}.GetSearchRecommendedBrand(robotID, "", 1)
	return list, err
}

// 获取最短路径 GetShortestPath
func (Robot) GetShortestPath(robotID, dstPositionID, dstFloorID, positionDesc string) (PathDetail, error) {
	// 获取位置id
	if dstPositionID == "" {
		dstPositionID = Robot{}.getDstPositionIDByName(robotID, positionDesc)
	}
	// 获取最短路径
	data, err := Nav{}.GetShortestPath(robotID, "", dstPositionID, dstFloorID)
	return data, err
}

// getDstPositionIDByName 通过位置名称获取位置id
func (Robot) getDstPositionIDByName(robotID, positionDesc string) (postionID string) {
	db := mysql.NewDB()
	// 根据robotID获取楼层id
	var (
		deviceRecord  models.Device
		keywordRecord models.PositionKeywords
	)
	db.Model(&models.Device{}).Where("robot_id = ?", robotID).Take(&deviceRecord)
	// 需要对positionDesc进行分词
	keywords := jieba.Jieba.Cut(positionDesc, true)
	for _, keyword := range keywords {
		var keywordRecord models.PositionKeywords
		// 查询关键字匹配的位置id
		found := db.Model(&models.PositionKeywords{}).
			Where("floor_id = ?", deviceRecord.FloorID).
			Where("keyword = ?", keyword).
			Take(&keywordRecord).
			RowsAffected
		if found > 0 {
			return keywordRecord.PositionID
		}
	}
	return keywordRecord.PositionID
}
